{
	UCE - cross-platform game engine written in delphi.
	Copyright (c) 2009, Shutko Vitaly
	
	This file is part of UCE.

	UCE is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	UCE is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
}

unit uTyp;

interface

const
  LIST_ITER = 8;

type
  TProc = procedure();

  TBytes = array of Byte;
	TStrings = array of String;

  TClr = packed record
     r, g, b, a: Byte;
  end;

  TPnt = record
    x, y: Integer;
  end;

  TListItem = class
    name: String;
  end;

  TList = class(TListItem)
    items: array of TListItem;
    size, realSize, factSize: Integer;

    function Add(item: TListItem): TListItem;
    function Find(name: String): TListItem; overload;
    function Find(item: TListItem): Integer; overload;
    function Rem(item: TListItem): Boolean;
    
    procedure Free_;

    constructor Create;
  end;

function MakeClr(r, g, b, a: Byte): TClr; overload;
function MakeClr(c: Cardinal): TClr; overload;
function MakePnt(x, y: Integer): TPnt;
function MakePath(path: String): String;
function MakeFourCC(c0, c1, c2, c3: Char): Cardinal;

function Explode(delim, str: String): TStrings;

implementation

function MakeClr(r, g, b, a: Byte): TClr;
begin
  Result.r := r;
  Result.g := g;
  Result.b := b;
  Result.a := a;
end;

function MakeClr(c: Cardinal): TClr;
begin
  Result.r := c and $FF;;
  Result.g := (c and $FF00) shr 8;
  Result.b := (c and $FF0000) shr 16;
  Result.a := (c and $FF000000) shr 24;
end;

function MakePnt(x, y: Integer): TPnt;
begin
  Result.x := x;
  Result.y := y;
end;

function MakePath(path: String): String;
{$IFDEF WIN32}
var
  i: Integer;
{$ENDIF}
begin
  Result := path;

{$IFDEF WIN32}
  for i := 1 to Length(Result) do
  	if Result[i] = '/' then Result[i] := '\';
{$ENDIF}
end;

function MakeFourCC(c0, c1, c2, c3: Char): Cardinal;
begin
  Result:= Byte(c0) or (Byte(c1) shl 8) or (Byte(c2) shl 16) or (Byte(c3) shl 24);
end;

function Explode(delim, str: String): TStrings;
var
  str2: String;
  i: Integer;
begin
  i := 0;
  str2 := str + delim;

  repeat
    SetLength(Result, i + 1);
    Result[i] := Copy(str2, 0, Pos(delim, str2) - 1);
    Delete(str2, 1, Length(Result[i]) + Length(delim));
    Inc(i);
  until str2 = '';
end;

constructor TList.Create;
begin
  size := 0;
  realSize := 0;
end;

function TList.Add(item: TListItem): TListItem;
var
  i: Integer;
begin
  Result := item;

  for i := 0 to size - 1 do
    if items[i] = nil then
    begin
      items[i] := item;
      Exit;
    end;

  if size = realSize then
  begin
    SetLength(items, size + LIST_ITER);
    realSize := size + LIST_ITER;
  end;

  items[size] := item;

  Inc(size);
  Inc(factSize);
end;

function TList.Find(name: String): TListItem;
var
  i: Integer;
begin
  Result := nil;

  for i := 0 to size - 1 do
    if items[i] <> nil then
      if items[i].name = name then
      begin
        Result := items[i];
        Exit;
      end;
end;

function TList.Find(item: TListItem): Integer;
var
  i: Integer;
begin
  Result := -1;

  for i := 0 to size - 1 do
    if items[i] = item then
    begin
      Result := i;
      Exit;
    end;
end;

function TList.Rem(item: TListItem): Boolean;
var
  i: Integer;
begin
  Result := False;

  for i := 0 to size - 1 do
    if items[i] = item then
    begin
      items[i] := nil;
      Dec(factSize);
      Result := True;
      Exit;
    end;
end;

procedure TList.Free_;
var
  i: Integer;
begin
  for i := 0 to size - 1 do
    if items[i] <> nil then items[i].Free;
end;

end.
